package com.zwj.batch;

import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;

/**
 * @author 张文接
 * @version 1.0
 * @createTime 2023-07-14 10:55:00
 */
@SpringBootTest(classes = BatchApplication.class)
public class Demo {
    public static void main(String[] args) {
        int[] arrs = {0, 1, 2, 3, 4, 5, 6, 7};
        pivotIndex(arrs);
    }

    public static int pivotIndex(int[] nums) {
        //先求出数组的总和
        int sum = Arrays.stream(nums).sum();
        int length = nums.length;
        //中位索引
        int middleIndex = 0;
        while (middleIndex < length) {
            int[] leftArrs = Arrays.copyOfRange(nums, 0, middleIndex);
            int[] rightArrs = Arrays.copyOfRange(nums, middleIndex + 1, length);
            int sumLeft = Arrays.stream(leftArrs).sum();
            int sumRight = Arrays.stream(rightArrs).sum();
            if (sumLeft == sumRight){
                break;
            }
            middleIndex++;
        }
        return middleIndex;
    }
}
